package com.xcm.sso.client.util;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
	****************************************************************************
 * 版本:  v1.0  2016年5月12日  (sj)
 * 版权所有:      成都联腾动力控制技术有限公司智能汽车事业部
 * 笔记:
 * 	1.v0.1 上午上午11:03:35  初始化文档
	****************************************************************************
 */
public class RestClient {
	public static final String SYSTEM_CHARSET = "UTF-8";
	
	public static final String SYSTEM_CHARSET_GBK ="GBK";
	
	private CloseableHttpClient httpclient = null;
	private Map<String, Object> param = new HashMap<String, Object>();
	private String targetUrl = null;
	
	private RestClient(){}
	
	public static RestClient newClient(){
		RestClient restClient = new RestClient();
		restClient.httpclient = HttpClientBuilder.create().build();
		return restClient;
	}
	
	public RestClient target(String url){
		this.targetUrl = url;
		return this;
	}
	
	public RestClient param(String key, Object value){
		param.put(key, value);
		return this;
	}
	
	public String post() {
		return post(targetUrl, param);
	}
	
	public String post_gbk(){
		return post(targetUrl, param,1);
	}
	
	/** 
	 * @param remoteUrl 远程url
	 * @param paramMap 需要传递的参数
	 * args 
	 * */
	public static String post(String remoteUrl, Map<String, Object> paramMap,int ... args) {
		/*if(args!=null ){ //使用gbk编码
			return post(remoteUrl, paramMap,SYSTEM_CHARSET_GBK);
		}*/
		return post(remoteUrl, paramMap, SYSTEM_CHARSET);
	}
	
	/** post多参数，支持String值、String[]值 */
	public static String post(String remoteUrl, Map<String, Object> paramMap, String responseCharset) {
		try {
			//构建post
			HttpPost httpPost = new HttpPost(new URL(remoteUrl).toURI());
			if (paramMap != null && paramMap.size() > 0) {	//post参数
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				for (String key : paramMap.keySet()) {
					Object value = paramMap.get(key);
					if (value instanceof Object[]) {	//数组值
						Object[] texts = (Object[])value;
						for (Object text : texts) {
							params.add(new BasicNameValuePair(key, text.toString()));
						}
					} else {
						String text = value.toString();
						params.add(new BasicNameValuePair(key, text));
					}
				}
				httpPost.setEntity(new UrlEncodedFormEntity(params, responseCharset));
			}

			//执行post
			CloseableHttpClient httpclient = HttpClientBuilder.create().build();
//			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("HTTP request Exception");
			}
			return EntityUtils.toString(response.getEntity(), responseCharset);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}


	public static String postByInputstream(String remoteUrl, byte[] bytes) {
		try{
			HttpPost httpPost = new HttpPost(remoteUrl);
			InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(bytes),-1);
			inputStreamEntity.setChunked(true);
			inputStreamEntity.setContentType("binary/octet-stream");
			httpPost.setEntity(inputStreamEntity);
			//执行post
			CloseableHttpClient httpclient = HttpClientBuilder.create().build();
//			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("HTTP request Exception");
			}
			return EntityUtils.toString(response.getEntity());
		}catch (Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static void main(String[] args) {
		
		String MessageContent = "发送测试短信内容为yaowm"; //短信内容
		String UserNumber = "13058694520,18030611590";
	     RestClient client = RestClient.newClient();
	     /*client.target(GetProperty.getPropertyByName("mobil","url"));
		 client.param("SpCode",GetProperty.getPropertyByName("mobil","SpCode"));
	     client.param("LoginName",GetProperty.getPropertyByName("mobil","LoginName"));
		 client.param("Password", GetProperty.getPropertyByName("mobil","Password"));
		 client.param("MessageContent",MessageContent);
		 client.param("UserNumber",UserNumber);
		 client.param("SerialNumber",String.valueOf(System.currentTimeMillis()));
		 client.param("f",GetProperty.getPropertyByName("mobil","f"));*/
		 client.post_gbk();
	}
	
	
	
	
	

}